package com.example.nft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.nft.dao.Tutu3Mapper;
import com.example.nft.entity.Tutu2;
import com.example.nft.entity.Tutu3;
import com.example.nft.service.Tutu3Service;
import io.swagger.annotations.Example;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Repository("Tutu3Service")
public class Tutu3ServiceImpl implements Tutu3Service {

    @Resource
    public Tutu3Mapper tutu3Mapper;

    private final static Logger logger = LoggerFactory.getLogger(Example.class);

    @Override
    public List<Tutu3> selectAll(String fenqu, String name) {
        QueryWrapper<Tutu3> queryWrapper = new QueryWrapper<>();
        if (fenqu!=null&&!("".equals(fenqu))) {
            queryWrapper.like ("fenqu", fenqu);
        }
        if (name!=null&&!("".equals(name))) {
            queryWrapper.like ("name", name);
        }

        List<Tutu3> tutus = tutu3Mapper.selectList (queryWrapper);
        return tutus;
    }

    @Override
    public List<Tutu3> selectAll() {
        return tutu3Mapper.selectList(null);
    }

    @Override
    public String insert() throws ParseException {
        String video1 =null;
        List<String> videohtmls;
        String url = "https://www.338e.net/";
        List<String> htmls = getHtmls (url);
        for (String s : htmls) {
            if (s.contains (":void(0)")){
                continue;
            }
            videohtmls = getVideoHtml (s);
            for (String videohtml : videohtmls) {
                if (s.contains (":void(0)")){
                    continue;
                }
                video1= getVideo(videohtml);
            }
            logger.info ("视频全部存入完成");
        }
        return "视频全部存入完成";
    }




    //获取视频详细内容
    public String getVideo(String html) throws ParseException {
        String html1 = getHtml(html);
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
        try {
            Document document = Jsoup.parse(html1);
            Elements wrap = document.select (".download");
            String title = document.getElementsByTag("title").get(0).text();
            Elements select = document.select (".pannel.clearfix");
            String data  =  select.select (".data").get (1).text ().replace ("时间：","");
            String fenqu = select.select (".data").get (2).text().replace ("分类：","");
            for (Element element : wrap) {
                Elements a = element.select ("a");
                for (Element element1 : a) {
                    String attr = element1.select ("a").attr ("href");
                    Tutu3 tutu3 = new Tutu3();
                    tutu3.setAddress(attr);
                    tutu3.setName(title);
                    tutu3.setDatatime (sdf.parse (data));
                    tutu3.setFenqu (fenqu);
                    tutu3Mapper.insert(tutu3);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return "插入成功";
    }
    //解析页面
    public static String getHtml(String url){
        String html = "";
        try {
            html = Jsoup.connect(url).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }
    //获取子页面的页面最大值
    public static Integer getindex(String html){
        try {
            String html1 = getHtml(html);
            String s ="https://www.338e.net";
            Document document = Jsoup.parse(html1);
            Elements a1 = document.select(".moble_pagination").select("a");
            String index =s+a1.get(a1.size() - 1).attr("href");
            String index_ = index.replace(html, "").replace("index_", "").replace(".html", "");
            logger.info ("最大值为"+index_);
            return Integer.valueOf(index_);
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        return 1;
    }
    //获取子页面的所有网页
    public static List<String> getVideoHtml(String html){
        List<String> list = null;
        try {
            Integer getindex = getindex(html);
            list = new ArrayList<> ();
            for (int i = 1; i<=getindex; i++){
                StringBuilder htmlBuilder = new StringBuilder(html);
                htmlBuilder.append("index_").append(i).append(".html");
                String s = htmlBuilder.toString();
                if (s.contains ("index_1")){
                    s = s.replace ("index_1.html", "");
                }
                s = getHtml(s);
                Document document = Jsoup.parse(s);
                Elements wrap = document.select (".preview-item");
                for (Element element : wrap) {
                    Elements a = element.select ("a");
                    for (Element element1 : a) {
                        String attr = element1.select ("a").attr ("href");
                        list.add ("https://www.338e.net"+attr);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return list;
    }
    //获取子页面
    public static List<String> getHtmls(String html){
        List<String> list = null;
        try {
            html = getHtml(html);
            list = new ArrayList<>();
            String s =null;
            Document document = Jsoup.parse(html);
            Elements wrap = document.select (".wrap");
            for (Element element : wrap) {
                Elements a = element.select ("a");
                for (Element element1 : a) {
                    String attr = element1.select ("a").attr ("href");
                    list.add ("https://www.338e.net"+attr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return list;
    }




}
